<template>
  <view class="login-container">
    <!-- 顶部渐变背景 -->
    <view class="top-background">
      <view class="logo-container">
        <view class="logo">
          <text class="logo-text">
            伟恒牧场TMR水分填报系统
          </text>
        </view>
      </view>
    </view>

    <!-- 登录表单 -->
    <view class="login-form">
      <!-- 用户名输入 -->
      <view class="input-group">
        <view class="input-icon">
          <image src="/static/images/用户.png" class="icon" />
        </view>
        <input
          v-model="username"
          class="login-input"
          type="text"
          placeholder="请输入用户名"
          placeholder-style="color: #999"
        >
      </view>

      <!-- 密码输入 -->
      <view class="input-group">
        <view class="input-icon">
          <image src="/static/images/隐私保护.png" class="icon" />
        </view>
        <input
          v-model="password"
          class="login-input"
          border="none"
          type="text"
          :password="true"
          placeholder="请输入密码"
          placeholder-style="color: #999"
        >
      </view>

      <!-- 确认密码输入 -->
      <!-- <view class="input-group">
        <view class="input-icon">
          <image src="/static/images/安全隐私.png" class="icon" />
        </view>
        <input
          v-model="code"
          class="login-input"
          type="text"
          placeholder="请输入验证码"
          placeholder-style="color: #999"
        >
        <text class="rmut-text">
          RMut
        </text>
      </view> -->

      <!-- 登录按钮 -->
      <button class="login-button" @tap="submit">
        登录
      </button>

      <!-- 底部链接 -->
      <!-- <view class="bottom-links">
        <text class="link" @tap="forgotPassword">
          忘记密码
        </text>
        <text class="link" @tap="registerNow">
          现在注册
        </text>
      </view> -->
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import useShare from '@/hooks/use-share';
import { HOME_PATH, isTabBarPath, LOGIN_PATH, removeQueryString } from '@/router';
import { useUserStore } from '@/store';

import { tranRSA } from '@/utils/common';

// 使用分享hook
const { onShareAppMessage, onShareTimeline } = useShare({
  title: '饲料检测历史记录',
  path: '/pages/tab/home/index',
  imageUrl: '/static/images/share.png',
});

const userStore = useUserStore();

// 必须显式调用才能正常分享（小程序平台要求）
onShareAppMessage();
onShareTimeline();

// 表单数据
const username = ref<string>('');
const password = ref<string>('');
// const code = ref<string>('');
let redirect = HOME_PATH;

// // 忘记密码处理
// function forgotPassword() {
//   uni.$u.toast('忘记密码功能待实现');
// }

// // 立即注册处理
// function registerNow() {
//   uni.$u.toast('注册功能待实现');
// }

// 登录处理
async function submit() {
  if (!username.value) {
    uni.$u.toast('请输入用户名');
    return;
  }
  if (!password.value) {
    uni.$u.toast('请输入密码');
    return;
  }
  // if (!code.value) {
  //   uni.$u.toast('请输入验证码');
  //   return;
  // }
  uni.showLoading({
    title: '登录中...',
  });
  // 登录请求
  const res = await userStore.login({
    account: username.value,
    password: tranRSA(password.value),
    loginType: 1,
  }).catch(() => {
    uni.$u.toast('登录失败');
  });
  if (!res) return;

  await userStore.getUserInfo({ account: username.value, menuTypes: [1, 2] });

  uni.hideLoading();

  // 保存用户名和密码到本地存储
  uni.setStorageSync('username', username.value);
  uni.setStorageSync('password', password.value);
  uni.$u.route({
    type: isTabBarPath(redirect) ? 'switchTab' : 'redirectTo',
    url: redirect,
  });
}

// 页面加载时获取重定向地址
onLoad((options: any) => {
  if (options.redirect && removeQueryString(options.redirect) !== LOGIN_PATH) {
    redirect = decodeURIComponent(options.redirect);
  }

  // 从本地存储读取已保存的用户名和密码
  const savedUsername = uni.getStorageSync('username');
  const savedPassword = uni.getStorageSync('password');

  if (savedUsername) {
    username.value = savedUsername;
  }
  if (savedPassword) {
    password.value = savedPassword;
  }
});
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #ffffff;
}

.top-background {
  height: 40vh;
  background: linear-gradient(135deg, #1e88e5, #42a5f5);
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

.logo-container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.logo {
  // width: 160rpx;
  height: 160rpx;
  // border-radius: 50%;
  // background-color: #ffffff;
  display: flex;
  justify-content: center;
  align-items: center;
  // box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.15);
}

.logo-text {
  font-size: 48rpx;
  font-weight: bold;
  color: #fff;
}

.login-form {
  padding: 60rpx 40rpx 40rpx;
  margin-top: -60rpx;
  background-color: #ffffff;
  border-top-left-radius: 30rpx;
  border-top-right-radius: 30rpx;
  box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.05);
  flex: 1;
}

.input-group {
  display: flex;
  align-items: center;
  border-bottom: 2rpx solid #e0e0e0;
  padding: 20rpx 0;
  margin-bottom: 40rpx;
  position: relative;
}

.input-icon {
  margin-right: 20rpx;
  width: 40rpx;
  text-align: center;
}
.icon {
  width: 48rpx;
  height: 48rpx;
}

.login-input {
  flex: 1;
  font-size: 28rpx;
  color: #333333;
  padding: 10rpx 0;
  border: 0;
}

.rmut-text {
  color: #42a5f5;
  font-size: 28rpx;
  margin-left: 20rpx;
}

.login-button {
  width: 100%;
  height: 90rpx;
  background-color: #1e88e5;
  color: #ffffff;
  font-size: 32rpx;
  font-weight: 500;
  border-radius: 45rpx;
  margin-top: 40rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 6rpx 16rpx rgba(30, 136, 229, 0.4);
  transition: all 0.3s ease;
}

.login-button:active {
  background-color: #1976d2;
  box-shadow: 0 4rpx 12rpx rgba(30, 136, 229, 0.3);
  transform: translateY(2rpx);
}

.bottom-links {
  display: flex;
  justify-content: space-between;
  margin-top: 40rpx;
  padding: 0 20rpx;
}

.link {
  font-size: 28rpx;
  color: #666666;
}
</style>
